package alit.generator.bl.generator;

import alit.generator.bl.generator.exception.ContextException;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Alexander Litvinenko
 * Date: 11/28/11
 * Time: 7:59 PM
 */
public class Context {
    private String tableName;
    private Table table = new Table();
    private int currentRow = 0;
    private String currentColumnName;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public Table getTable() {
        return table;
    }

    public int getCurrentRow() {
        return currentRow;
    }

    public void setCurrentRow(int currentRow) {
        this.currentRow = currentRow;
    }

    public String getCurrentColumnName() {
        return currentColumnName;
    }

    public void setCurrentColumnName(String currentColumnId) {
        this.currentColumnName = currentColumnId;
    }

    public String getCurrentValueByColumnName(String name) {
        if (!table.isExistsName(name)) {
            throw new ContextException(String.format("Column with id '%s' doesn't exist.", name));
        }
        return table.getCell(name, getCurrentRow());
    }

    public class Table {
        Map<String, String[]> name2data = new HashMap<String, String[]>();

        public void createColumn(String name, int n) {
            name2data.put(name, new String[n]);
        }

        public String getCell(String name, int row) {
            return name2data.get(name)[row];
        }

        public void setCell(String name, int row, String value) {
            name2data.get(name)[row] = value;
        }

        public boolean isExistsName(String name) {
            return name2data.containsKey(name);
        }
    }
}
